package com.jyw.business.obstacles.expand.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyw.business.de.constant.DemandSourceConstant;
import com.jyw.business.de.constant.DemandTypeConstant;
import com.jyw.business.de.domain.dto.web.ObsDemandAddDto;
import com.jyw.business.de.domain.dto.web.ObsTaskAddDto;
import com.jyw.business.de.service.IDeDemandInfoService;
import com.jyw.business.de.service.IDeTaskInfoService;
import com.jyw.business.obstacles.constant.OrderCurrStateConstant;
import com.jyw.business.obstacles.constant.OrderMakeTypeConstant;
import com.jyw.business.obstacles.constant.OrderProcessStateConstant;
import com.jyw.business.obstacles.domain.ObsOrderFault;
import com.jyw.business.obstacles.domain.dto.web.OrderProcessRecordDto;
import com.jyw.business.obstacles.service.IObsOrderFaultService;
import com.jyw.business.obstacles.service.IObsOrderInfoService;
import com.jyw.business.obstacles.service.IObsOrderProcessService;
import com.jyw.common.exception.ValidationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ObsDemandServiceImpl implements IObsDemandService{

    @Resource
    private IDeDemandInfoService deDemandInfoService;

    @Resource
    private IObsOrderFaultService orderFaultService;

    @Resource
    private IDeTaskInfoService taskInfoService;

    @Resource
    private IObsOrderProcessService processService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transferDemand(ObsDemandAddDto orderDto) {
        //检查是否能够转需求
        ObsOrderFault fault = orderFaultService.getFaultByOrder(orderDto.getOrderNum());
        if (ObjectUtils.isEmpty(fault)) {
            throw new ValidationException("未找到工单");
        }
        if (!Arrays.asList(OrderCurrStateConstant.PROCESSING,OrderCurrStateConstant.NOT_PASS_AUDIT).contains(fault.getCurrState())) {
            throw new ValidationException("工单不能转需求");
        }
        //入库
        orderFaultService.lambdaUpdate().eq(ObsOrderFault::getId, fault.getId()).set(ObsOrderFault::getCurrState, OrderCurrStateConstant.END_DEMAND).update();

        //子工单结束
        List<ObsOrderFault> childOrder = orderFaultService.list(new QueryWrapper<ObsOrderFault>().setEntity(new ObsOrderFault() {{
            setParentOrderNum(orderDto.getOrderNum());
        }}));
        orderFaultService.updateBatchById(childOrder.stream().map(data -> {
            data.setCurrState(OrderCurrStateConstant.ENDED);
            return data;
        }).collect(Collectors.toList()));

        //设置需求类型和来源
        orderDto.getDemandAdd().setDemandSource(DemandSourceConstant.ORDER);
        orderDto.getDemandAdd().setDemandType(DemandTypeConstant.ORDER);
        //设置需要提交
        orderDto.getDemandAdd().setSubmit(true);

        //新增需求
        deDemandInfoService.insertDeDemandInfo(orderDto.getDemandAdd());

        //记录流程
        processService.record(OrderProcessRecordDto
                .builder()
                .orderNum(fault.getOrderNum())
                .lastFaultState(fault.getCurrState())
                .operState(OrderProcessStateConstant.MAKING)
                .makeState(OrderMakeTypeConstant.END_DEMAND)
                .processValue(JSONObject.toJSONString(orderDto.getDemandAdd()))
                .build()
        );
    }

    @Override
    public void transferTask(ObsTaskAddDto orderDto) {
        //检查是否能够转需求
        ObsOrderFault fault = orderFaultService.getFaultByOrder(orderDto.getOrderNum());
        if (ObjectUtils.isEmpty(fault)) {
            throw new ValidationException("未找到工单");
        }
        if (!Arrays.asList(OrderCurrStateConstant.PROCESSING,OrderCurrStateConstant.NOT_PASS_AUDIT).contains(fault.getCurrState())) {
            throw new ValidationException("工单不能转任务");
        }
        //入库
        orderFaultService.lambdaUpdate().eq(ObsOrderFault::getId, fault.getId()).set(ObsOrderFault::getCurrState, OrderCurrStateConstant.END_TASK).update();

        //子工单结束
        List<ObsOrderFault> childOrder = orderFaultService.list(new QueryWrapper<ObsOrderFault>().setEntity(new ObsOrderFault() {{
            setParentOrderNum(orderDto.getOrderNum());
        }}));
        orderFaultService.updateBatchById(childOrder.stream().map(data -> {
            data.setCurrState(OrderCurrStateConstant.ENDED);
            return data;
        }).collect(Collectors.toList()));

        //新增需求
        taskInfoService.orderAddTask(orderDto.getTaskAdd());
        //记录流程
        processService.record(OrderProcessRecordDto
                .builder()
                .orderNum(fault.getOrderNum())
                .lastFaultState(fault.getCurrState())
                .operState(OrderProcessStateConstant.MAKING)
                .makeState(OrderMakeTypeConstant.END_TASK)
                .processValue(JSONObject.toJSONString(orderDto.getTaskAdd()))
                .build()
        );
    }
}
